<template>
	<div class="login_div">
		<el-card class="box-card login_style">
			<div slot="header" class="clearfix" style="text-align: center;">
				<span>用户登录页面</span>
			</div>
			<!--form表单-->
			<el-form :model="login" :rules="rules" ref="ruleForm" label-width="10px" class="demo-ruleForm">
				<el-form-item prop="username">
					<el-input v-model="login.username" placeholder="用户名" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input v-model="login.password" placeholder="密码" show-password></el-input>
				</el-form-item>
				<el-form-item prop="code">
					<el-input v-model="login.code" placeholder="验证码" style="width: 250px;"></el-input>
					<img :src="img_code" alt="" style="vertical-align:middle;margin-left:20px">
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="submitForm('ruleForm')" style="width: 370px;">登录</el-button>
				</el-form-item>
			</el-form>

			<div>

			</div>
		</el-card>
	</div>

</template>

<script>
	import {message} from 'element-ui'
	export default {
		data() {
			return {
				img_code: '',
				login: {
					username: "",
					password: "",
					code: ""
				},
				rules: {
					username: [{
						required: true,
						message: '请输入用户名',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: 'blur'
					}]
				}
			}
		},
		mounted() {
			this.img_code = '/tAdmin/verifyCode?time' + new Date;
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					this.$postRequest("/tAdmin/login", this.login).then(a => {
						const {code, success, data, message} = a;
						if(success) {
							var admin = data.user;
							console.log(admin);
							//保存用户信息
							sessionStorage.setItem("user", JSON.stringify(admin));
							this.$router.push("/main");
						} else {
							message.error(message)
						}
					});

				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style>
	.login_style {
		width: 420px;
		height: 350px;
		border-radius: 5p%;
	}

	.login_div {
		display: flex;
		/*水平居中*/
		justify-content: center;
		/*垂直居中*/
		align-items: center;
		height: 100vh;
	}
</style>